package logic

import (
	"context"
	"srv/house"

	"api/internal/svc"
	"api/internal/types"
	jwt "github.com/golangblogs/gojwt"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.HouseSrv.UserLogin(l.ctx, &house.UserLoginRequest{
		Phone: req.Phone,
		Code:  req.Code,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	claims := jwt.CustomClaims{ID: uint(login.UserId)}
	newJWT := jwt.NewJWT("token")
	token, _ := newJWT.CreateToken(claims)
	return &types.Response{
		Code: 200,
		Msg:  "用户登录成功",
		Data: map[string]interface{}{
			"token": token,
		},
	}, nil
}
